package com.nwpu.coursesupport.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.nwpu.coursesupport.criteria.LessonSearchCriteria;
import com.nwpu.coursesupport.entity.bean.JSONResult;
import com.nwpu.coursesupport.enumeration.LessonType;
import com.nwpu.coursesupport.service.imple.LessonService;
import com.nwpu.coursesupport.service.param.LessonServiceParam;
import com.nwpu.coursesupport.tool.StringUtil;

@Controller
@RequestMapping("/lesson")
public class LessonController extends AbstractController {
	
	@Autowired
	private LessonService lessonService;

    @RequestMapping("/detail")
    public String detail(@RequestParam("id") Integer id,HttpServletRequest request) {
        JSONResult result = invokeService("lesson", "detail", "{\"id\":\"" + id + "\"}");
        request.setAttribute("data", this.parseData(result));
        return "/teapage/lessonDetail";
    }

    @RequestMapping("/ajaxDetail")
    @ResponseBody
    public JSONResult ajaxDetail(@RequestParam("id") Integer id) {
        JSONResult result = invokeService("lesson", "detail", "{\"id\":\"" + id + "\"}");
        return result;
    }
    
    @RequestMapping("/ajaxDetailView")
    @ResponseBody
    public JSONResult ajaxDetailView(@RequestParam("id") Integer id) {
        JSONResult result = invokeService("lesson", "detailView", "{\"id\":\"" + id + "\"}");
        return result;
    }
    
    @RequestMapping("/list")
    public String list(@RequestParam("classId")int classId,Integer pageNo,String type,HttpServletRequest request) {
    	LessonServiceParam.SearchParam p =new LessonServiceParam.SearchParam();
    	p.setPageNo(pageNo==null?1:pageNo);
    	LessonSearchCriteria sc=new LessonSearchCriteria();
    	sc.setClassId(classId);
    	p.setSearch(sc);
    	type=(type==null?LessonType.THEORY.name():type);
    	if(type.equals(LessonType.LAB.name())){
    		sc.setType(LessonType.LAB);
    	}else{
    		sc.setType(LessonType.THEORY);
    	}
    	JSONResult result = this.lessonService.listData(p);
        request.setAttribute("data", this.parseData(result));
        return "/teapage/lessonList";
    }

    @RequestMapping("/ajaxListData")
    @ResponseBody
    public JSONResult ajaxListData(@RequestParam("data") String data) {
        JSONResult result = invokeService("lesson", "listData", data);
        return result;
    }
    
    @RequestMapping("/listView")
    public String listView(@RequestParam("id")Integer classId,Integer pageNo,String type,HttpServletRequest request) {
    	LessonServiceParam.SearchParam p =new LessonServiceParam.SearchParam();
    	p.setPageNo(pageNo==null?1:pageNo);
    	LessonSearchCriteria sc=new LessonSearchCriteria();
    	sc.setClassId(classId);
    	p.setSearch(sc);
    	type=(type==null?LessonType.THEORY.name():type);
    	if(type.equals(LessonType.LAB.name())){
    		sc.setType(LessonType.LAB);
    	}else{
    		sc.setType(LessonType.THEORY);
    	}
    	JSONResult result = this.lessonService.listView(p);
        request.setAttribute("data", this.parseData(result));
        return "/teapage/classIndex";
    }
    
    @RequestMapping("/ajaxListView")
    @ResponseBody
    public JSONResult ajaxListView(@RequestParam("data") String data) {
        JSONResult result = invokeService("lesson", "listView", data);
        return result;
    }

    @RequestMapping("/ajaxCreate")
    @ResponseBody
    public JSONResult ajaxCreate(@RequestParam("data") String data) {
        JSONResult result = invokeService("lesson", "create", data);
        return result;
    }

    @RequestMapping("/ajaxUpdate")
    @ResponseBody
    public JSONResult ajaxUpdate(@RequestParam("data") String data) {
    	JSONResult result = invokeService("lesson", "update", data);
        return result;
    }

    @RequestMapping("/ajaxDelete")
    @ResponseBody
    public JSONResult ajaxDelete(@RequestParam("id") String id) {
    	JSONResult result = invokeService("lesson", "remove", "{\"id\":\"" + id + "\"}");
        return result;
    }

    @RequestMapping("/ajaxTeacherCLesson")
    @ResponseBody
    public JSONResult ajaxTeacherCurrentLesson(String data) {
    	if(StringUtil.isEmpty(data)){
    		int id=(int) getSession().getAttribute("id");
    		data="{\"id\":\""+id+"\"}";
    	}
    	JSONResult result = invokeService("lesson", "currentLessonByTeacher", data);
        return result;
    }
    
    @RequestMapping("/ajaxAssistantCLesson")
    @ResponseBody
    public JSONResult ajaxAssistantCurrentLesson(String data) {
    	if(StringUtil.isEmpty(data)){
    		int id=(int) getSession().getAttribute("id");
    		data="{\"id\":\""+id+"\"}";
    	}
    	JSONResult result = invokeService("lesson", "currentLessonByAssistant", data);
        return result;
    }
    
    @RequestMapping("/ajaxStudentCLesson")
    @ResponseBody
    public JSONResult ajaxStudentCurrentLesson(String data) {
    	if(StringUtil.isEmpty(data)){
    		int id=(int) getSession().getAttribute("id");
    		data="{\"id\":\""+id+"\"}";
    	}
    	JSONResult result = invokeService("lesson", "currentLessonByStudent", data);
        return result;
    }
    
    @RequestMapping("/studentCLesson")
    public String studentCurrentLesson(HttpServletRequest request) {
    	int id=(int) getSession().getAttribute("id");
    	String data="{\"id\":\""+id+"\"}";
    	JSONResult result = invokeService("lesson", "currentLessonByStudent", data);
    	request.setAttribute("data", this.parseData(result));
    	return "/stupage/studentIndex";
    }
    
    /*@RequestMapping("/ajaxDeleteAll")
    @ResponseBody
    public JSONResult ajaxDeleteAll(@RequestParam("data") String data) {
    	JSONResult result = invokeService("lesson", "removeAll", data);
        return result;
    }*/



}
